/*
 ============================================================================
 Name		: FaceAuthetificator.cpp
 Author	  : Lenka Litvova
 Version	 : 1.0
 Copyright   : Your copyright notice
 Description : CFaceAuthetificator implementation
 ============================================================================
 */

#include "FaceAuthetificator.h"
#include "Globals.h"
#include <f32file.h>

CFaceAuthetificator::CFaceAuthetificator(CFaceGraph* aActualFace, MAuthetificationObserver* aObserver) :
	CActive(EPriorityStandard),// Standard priority
	iActualFace(aActualFace),
	iObserver(aObserver)
	{
	}

CFaceAuthetificator* CFaceAuthetificator::NewLC(CFaceGraph* aActualFace, MAuthetificationObserver* aObserver)
	{
	CFaceAuthetificator* self = new (ELeave) CFaceAuthetificator(aActualFace, aObserver);
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}

CFaceAuthetificator* CFaceAuthetificator::NewL(CFaceGraph* aActualFace, MAuthetificationObserver* aObserver)
	{
	CFaceAuthetificator* self = CFaceAuthetificator::NewLC(aActualFace, aObserver);
	CleanupStack::Pop(); // self;
	return self;
	}

void CFaceAuthetificator::ConstructL()
	{
		CActiveScheduler::Add(this); // Add to scheduler
	}

CFaceAuthetificator::~CFaceAuthetificator()
	{
	Cancel(); // Cancel any request, if outstanding
	
	// Delete instance variables if any
	//delete iActualFace;
	delete iFiles;
	}

void CFaceAuthetificator::DoCancel()
	{
		iCancel = ETrue;
	}

void CFaceAuthetificator::StartL()
	{
	Cancel(); // Cancel any request, just to be sure
	iCancel = EFalse;
	
	TRequestStatus* status=&iStatus;
	User::RequestComplete(status, KErrNone);
	GetGraphListL();
	iCounter = 0;
	
	SetActive(); // Tell scheduler a request is active
	}

void CFaceAuthetificator::RunL()
	{
		TInt error = iStatus.Int();
		if(!iCancel)
		{
			if(error != KErrNone || iCounter >= iFiles->Count())
			{
					//TRequestStatus* status=&iStatus;
					//User::RequestComplete(status, error);
					iObserver->AuthetificationFinishedL(EFalse);
			}
			else 
			{	
				TBool rec;
				TRAPD(err1, rec = AuthetificateL(iCounter));
				if(err1 != KErrNone)
				{
					TRequestStatus* status=&iStatus;
					User::RequestComplete(status, err1);
					SetActive();// znovu aktivuj tento aktivny objekt
				}
				else
				{
					if(!rec)
					{	
						iCounter++;
						TRequestStatus* status=&iStatus;
						User::RequestComplete(status, KErrNone);
						SetActive();// znovu aktivuj tento aktivny objekt
					}
					else
					{
						//TRequestStatus* status=&iStatus;
						//User::RequestComplete(status, KErrNone);
						iObserver->AuthetificationFinishedL(ETrue);
					}
				}
			}
		}
	}

TInt CFaceAuthetificator::RunError(TInt aError)
	{
	return aError;
	}

void CFaceAuthetificator::GetGraphListL()
	{
		_LIT(KFiles, "*");
		CDir* dirs = 0;
	    
	    TBufC<50> buf(KHomeDir);
	   	TPtr graph_dir = buf.Des();
	   	graph_dir.Append(_L("graphs\\"));
	   	
	   	RFs fileserver;
	   	User::LeaveIfError(fileserver.Connect());
	   	CleanupClosePushL(fileserver);
	 
	    User::LeaveIfError(fileserver.GetDir(graph_dir, KEntryAttNormal, ESortByName, iFiles, dirs));
	      
	     TFindFile findFiles(fileserver);
	     TInt err = findFiles.FindWildByDir( KFiles, graph_dir, iFiles );
	 
	     CleanupStack::PushL( dirs );
	 
	     CleanupStack::PopAndDestroy(dirs);

	     //closing fileserver connection
	     fileserver.Close();
	     CleanupStack::PopAndDestroy(&fileserver); // destroy fs
	}

TBool CFaceAuthetificator::AuthetificateL(TInt aGraph)
	{
		CFaceGraph *g = CFaceGraph::NewL();
		TEntry a = (*iFiles)[aGraph];
		TBufC<50> buf(KHomeDir);
		TPtr graph = buf.Des();
	    graph.Append(_L("graphs\\"));
	    graph.Append(a.iName);
		g->LoadGraph(graph);
		
		TInt failed = g->IsSame(iActualFace);
			  
		TReal sim = g->GetJetSimilarity(iActualFace)*100;
			    
		if((failed <= 1 && sim > 92)  || (failed <= 3 && sim > 96))
	    {
			return ETrue;
	    }
	    if(sim > 94 && failed < 3)
	    {
			return ETrue;
	    }

	    return EFalse;
	}

TBufC<KMaxFileName> CFaceAuthetificator::GetAuthentificatedUser()
	{
		return ((*iFiles)[iCounter]).iName;
	}
